package com.siyu.tickets.manager.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.siyu.common.hibernate3.Updater;
import com.siyu.common.page.Pagination;
import com.siyu.common.security.encoder.PwdEncoder;
import com.siyu.tickets.dao.PartnerDao;
import com.siyu.tickets.entity.Partner;
import com.siyu.tickets.manager.PartnerMng;

@Service
@Transactional
public class PartnerMngImpl implements PartnerMng {

	public Pagination getPage(String name, Boolean enable, int pageNo, int pageSize) {
		Pagination page = dao.getPage(name, enable, pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public Partner findById(Integer id) {
		Partner entity = dao.findById(id);
		return entity;
	}

	public Partner registPartner(Partner bean) {
		Date now = new Date();
		bean.getUser().setPassword(pwdEncoder.encodePassword(bean.getUser().getPassword()));
		bean.getUser().setRealname(bean.getTitle());
		bean.getUser().setCreateTime(now);
		bean.getUser().setLoginCount(0);
		bean.getUser().setActivation(true);
		bean.getUser().setEnable(true);

		bean.setMoney(new BigDecimal(0));
		return dao.save(bean);
	}

	public Partner updatePartner(Partner bean) {
		Partner oldBean = dao.findById(bean.getId());
		if (!StringUtils.isBlank(bean.getUser().getPassword())) {// 修改密码
			oldBean.getUser().setPassword(pwdEncoder.encodePassword(bean.getUser().getPassword()));
		}
		oldBean.getUser().setRealname(bean.getTitle());
		oldBean.getUser().setEnable(bean.getUser().getEnable());
		oldBean.getUser().setEmail(bean.getUser().getEmail());

		oldBean.setMaster(bean.getMaster());
		oldBean.setAddress(bean.getAddress());
		oldBean.setImgLicense(bean.getImgLicense());
		oldBean.setImgPhoto(bean.getImgPhoto());
		oldBean.setMobile(bean.getMobile());
		oldBean.setPhone(bean.getPhone());
		oldBean.setQq(bean.getQq());
		oldBean.setTitle(bean.getTitle());
		oldBean.setApiUrl(bean.getApiUrl());
		oldBean.setGender(bean.getGender());

		Updater<Partner> updater = new Updater<Partner>(oldBean);
		Partner entity = dao.updateByUpdater(updater);
		return entity;
	}

	public Partner updateByUpdater(Updater<Partner> updater) {
		return dao.updateByUpdater(updater);
	}

	public Partner deleteById(Integer id) {
		return dao.deleteById(id);
	}

	public Partner[] deleteByIds(Integer[] ids) {
		Partner[] beans = new Partner[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	public Partner[] enableByIds(Integer[] ids, Boolean enable) {
		Partner[] beans = new Partner[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			Partner bean = findById(ids[i]);
			if (enable == null) {
				bean.getUser().setEnable(!bean.getUser().getEnable());
			} else {
				bean.getUser().setEnable(enable);
			}
			beans[i] = bean;
		}
		return beans;
	}

	@Autowired
	private PartnerDao dao;

	@Autowired
	private PwdEncoder pwdEncoder;

}
